package fun.flyee.android.pengshan;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSONObject;

import fun.flyee.android.pengshan.models.UserInfo;
import fun.flyee.android.pengshan.utils.Logger;

public class Storage {

    public final static String TOKEN = "token";

    public static UserInfo getUser() {
        try {
            UserInfo user = JSONObject.parseObject(get("userInfo"), UserInfo.class);
            return user;
        } catch (Exception e) {
            return null;
        }
    }

    public static String get(String name) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        String ret = sp.getString(name, null);
        return ret;
    }

    public static void set(String name, String value) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(name, value);
        editor.commit();
    }

    public static void del(String name) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(name);
        editor.commit();
    }

    public static String get2(String name) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("global", Context.MODE_PRIVATE);
        String ret = sp.getString(name, null);
        return ret;
    }

    public static void set2(String name, String value) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("global", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(name, value);
        editor.commit();
    }

    public static void del2(String name) {
        try {
            SharedPreferences sp = AppContex.getContext().getSharedPreferences("global", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.remove(name);
            editor.commit();
        } catch (Exception e) {
            Logger.e("remove global cache error", e);
        }
    }

    public static void clear() {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    public static int getLastVersion() {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("sys", Context.MODE_PRIVATE);
        return sp.getInt("last-version", 0);
    }

    public static void setCurrentVersion(int version) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("sys", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("last-version", version);
        editor.commit();
    }

    public static long getLastTraceTime() {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        return sp.getLong("last-trace-time", 0l);
    }

    public static void setLastTraceTime(long version) {
        SharedPreferences sp = AppContex.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong("last-trace-time", version);
        editor.commit();
    }

}
